﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;

namespace MasApp.DAL
{
    /// <summary>
    ///Delete 的摘要说明
    /// </summary>
    public class Delete
    {
        public Delete()
        {
            //
            //TODO: 在此处添加构造函数逻辑
            //
        }
        public static int ExecuteNonQuery(string sql)
        {
            SqlCommand cmd = MasApp.DbUtil.Db.conn();
            cmd.CommandText = sql;
            int i=new int();
            try
            {
                cmd.Connection.Open();
                i = cmd.ExecuteNonQuery();
                cmd.Connection.Close();
            }
            catch (Exception ee)
            {
            }
            return i;
        }
        public static bool SetLoginFalse(string username)
        {
            SqlCommand cmd = MasApp.DbUtil.Db.conn();
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText = "SetLoginFalse";
            //SqlParameter p_username = new SqlParameter("@username", SqlDbType.VarChar,50);
            //p_username.Value = username;
            cmd.Parameters.Add("@username", SqlDbType.VarChar, 50).Value = username;
            //cmd.Parameters.AddWithValue(p_username, username);
            try
            {
                cmd.Connection.Open();
                if (cmd.ExecuteNonQuery() > 0)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch (Exception ee)
            {
                return false;
            }
        }
        public static bool DeleteOrgLogin(string username)
        {
            SqlCommand cmd = MasApp.DbUtil.Db.conn();
            string str = "UPDATE login SET available = 'false' WHERE username = '" + username + "'";
            cmd.CommandText = str;
            try
            {
                cmd.Connection.Open();
                if (cmd.ExecuteNonQuery() == 1)
                {
                    return true;
                }
                else { return false; }
            }
            catch (Exception ee)
            { return false; }
        }
        public static bool DeleteUser(string username)
        {
            SqlCommand cmd = MasApp.DbUtil.Db.conn();
            string str = "DELETE FROM users WHERE (username = '" + username + "')";
            cmd.CommandText = str;
            int i = 0;
            try
            {
                cmd.Connection.Open();
                i = cmd.ExecuteNonQuery();
                cmd.Connection.Close();
            }
            catch (Exception ee)
            {
                //System.Web.HttpContext.Current.Response.Redirect("~/showerror.aspx");
            }
            return (i > 0) ? true : false;
        }
        public static bool DeleteLogin(string username)
        {
            SqlCommand cmd = MasApp.DbUtil.Db.conn();
            string str = "DELETE FROM login WHERE (username = '" + username + "')";
            cmd.CommandText = str;
            int i = 0;
            try
            {
                cmd.Connection.Open();
                i = cmd.ExecuteNonQuery();
                cmd.Connection.Close();
            }
            catch (Exception ee)
            {
                //System.Web.HttpContext.Current.Response.Redirect("~/showerror.aspx");
            }
            return (i > 0) ? true : false;
        }
        public static bool DeleteUserrole(int userid)
        {
            SqlCommand cmd = MasApp.DbUtil.Db.conn();
            string str = "DELETE FROM userrole WHERE (userid = " + userid + ")";
            cmd.CommandText = str;
            int i = 0;
            try
            {
                cmd.Connection.Open();
                i = cmd.ExecuteNonQuery();
                cmd.Connection.Close();
            }
            catch (Exception ee)
            {
                //System.Web.HttpContext.Current.Response.Redirect("~/showerror.aspx");
            }
            return (i > 0) ? true : false;
        }
        public static int DeleteUserroleByUrid(int userroleid)
        {
            SqlCommand cmd = MasApp.DbUtil.Db.conn();
            string str = "DELETE FROM userrole WHERE (userroleid = " + userroleid + ")";
            cmd.CommandText = str;
            int i = 0;
            try
            {
                cmd.Connection.Open();
                i = cmd.ExecuteNonQuery();
                cmd.Connection.Close();
            }
            catch (Exception ee)
            {
                //System.Web.HttpContext.Current.Response.Redirect("~/showerror.aspx");
            }
            return i;
        }
        public static bool DeleteIndicator(int ruleid)
        {
            SqlCommand cmd = MasApp.DbUtil.Db.conn();
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText = "DeleteIndicator";
            cmd.Parameters.Add("@ruleid", SqlDbType.Int).Value = ruleid;
            int i = 0;
            try
            {
                cmd.Connection.Open();
                i = cmd.ExecuteNonQuery();
                cmd.Connection.Close();
            }
            catch (Exception ee)
            {
            }
            return i > 0 ? true : false;
        }
        public static bool DeleteRule(int modelid)
        {
            SqlCommand cmd = MasApp.DbUtil.Db.conn();
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText = "DeleteIndicator";
            cmd.Parameters.Add("@modelid", SqlDbType.Int).Value = modelid;
            int i = 0;
            try
            {
                cmd.Connection.Open();
                i = cmd.ExecuteNonQuery();
                cmd.Connection.Close();
            }
            catch (Exception ee)
            {
            }
            return i > 0 ? true : false;
        }
        public static bool DeleteModel(int modelid)
        {
            SqlCommand cmd = MasApp.DbUtil.Db.conn();
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText = "DeleteModel";
            cmd.Parameters.Add("@modelid", SqlDbType.Int).Value = modelid;
            int i = 0;
            try
            {
                cmd.Connection.Open();
                i = cmd.ExecuteNonQuery();
                cmd.Connection.Close();
            }
            catch (Exception ee)
            {
            }
            return i > 0 ? true : false;
        }
        public static bool DeleteMethod(int methodid)
        {
            SqlCommand cmd = MasApp.DbUtil.Db.conn();
            cmd.CommandText = "DeleteMethod";
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add("@methodid", SqlDbType.Int).Value = methodid;
            int i = 0;
            try
            {
                cmd.Connection.Open();
                i = cmd.ExecuteNonQuery();
                cmd.Connection.Close();
            }
            catch
            {
            }
            return i > 0 ? true : false;
        }
        public static int DeleteLevelMission(Model.levelmission lm)
        {
            SqlCommand cmd = MasApp.DbUtil.Db.conn();
            string str = "DELETE FROM levelmission WHERE (projectid = " + lm.Projectid 
                + " AND actor='" + lm.Actor + "')";
            cmd.CommandText = str;
            int i = 0;
            try
            {
                cmd.Connection.Open();
                i = cmd.ExecuteNonQuery();
                cmd.Connection.Close();
            }
            catch (Exception ee)
            {
                //System.Web.HttpContext.Current.Response.Redirect("~/showerror.aspx");
            }
            return i;
        }
        public static bool DeleteLevel(int methodid)
        {
            SqlCommand cmd = MasApp.DbUtil.Db.conn();
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText = "DeleteLevel";
            cmd.Parameters.Add("@methodid", SqlDbType.Int).Value = methodid;
            int i = 0;
            try
            {
                cmd.Connection.Open();
                i = cmd.ExecuteNonQuery();
                cmd.Connection.Close();
            }
            catch
            {
            }
            return i > 0 ? true : false;
        }

        public static int DeleteProject(int projectid)
        {
            SqlCommand cmd = MasApp.DbUtil.Db.conn();
            string str = "DELETE FROM project WHERE (projectid = " + projectid + ")";
            cmd.CommandText = str;
            int i = 0;
            try
            {
                cmd.Connection.Open();
                i = cmd.ExecuteNonQuery();
                cmd.Connection.Close();
            }
            catch (Exception ee)
            {
                //System.Web.HttpContext.Current.Response.Redirect("~/showerror.aspx");
            }
            return i;
        }
    }
}